/**
 * 页码减一
 * @param pagination 分页数据
 * @param data data数据源
 * @param selected 选中数据(多选时使用)
 */
export function decPageByData(
  pagination: Pagination,
  data: RefSource<any[]>,
  selected?: RefSource<any[]>
) {
  const isLastPage = pagination.page * pagination.limit >= pagination.total
  if (!isLastPage) return
  data = getSource(data)
  const page = pagination.page
  const dataLength = data.value.length
  const selectedLength = selected ? getSource(selected).value.length ?? 1 : 1
  const condition = page > 1 && selectedLength === dataLength
  if (condition) pagination.page--
}

/**
 * 处理排序参数
 * @param param0 table 排序原始参数
 * @returns
 */
export function getSortValue({
  order,
  prop
}: {
  prop: string
  order: string | null
}) {
  if (order === 'descending') order = 'desc'
  if (order === 'ascending') order = 'asc'
  const sort = order ? prop : null
  return { sort, order: order as Order }
}
